//
// Created by hunan on 25-7-27.
//

#ifndef LOOP_POOL_H
#define LOOP_POOL_H
#include <memory>
#include <mutex>
#include <thread>
#include <vector>

class Loop;
class IoReuseModuleFactory;
class TimerTask;
class LoopPool
{
private:
    struct LoopData
    {
        std::shared_ptr<Loop>      m_loop;
        std::shared_ptr<TimerTask> m_schedule_event;
        std::thread                m_thread;
        bool                       m_status;
    };
public:
    LoopPool();
    ~LoopPool();
    bool Initialize(int num, IoReuseModuleFactory* factory);
    Loop * GetLoop(int index) const;
    void   StopPool() const;
private:
    void StartThread(const std::shared_ptr<LoopData> & loop_data);
    void Schedule(const std::shared_ptr<LoopData>& loop_data);
private:
    std::mutex m_mutex;
    std::vector<std::shared_ptr<LoopData>> m_loop_pool;
};


#endif //LOOP_POOL_H
